home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 010a / fff361.zip / MATCH.C < prev    next >
C/C++ Source or Header  |  1991-09-06  |  2KB  |  91 lines

  1. #include <stdlib.h>
  2. #include <string.h>
  3.  
  4. static int      I_Match(char *Str, char *Pat);
  5. static int      S_Match(char *S, char *P, int Anchor);
  6.  
  7.  
  8.  int
  9. Match (char *Str, char *Pat) {
  10.     char            S_Name[66], S_Ext[4];
  11.     char            P_Name[66], P_Ext[4];
  12.     char           *p1;
  13.  
  14.     if ((p1 = strrchr(Str, '.')) != NULL) {
  15.         *p1 = '\0';
  16.         strcpy(S_Name, Str);
  17.         strcpy(S_Ext, p1 + 1);
  18.         *p1 = '.';
  19.         }
  20.     else {
  21.         strcpy(S_Name, Str);
  22.         S_Ext[0] = '\0';
  23.         }
  24.  
  25.     if ((p1 = strchr(Pat, '.')) != NULL) {
  26.         *p1 = '\0';
  27.         strcpy(P_Name, Pat);
  28.         strcpy(P_Ext, p1 + 1);
  29.         *p1 = '.';
  30.         }
  31.     else {
  32.         strcpy(P_Name, Pat);
  33.         strcpy(P_Ext, "*");
  34.         }
  35.  
  36.     if (!I_Match(S_Name, P_Name)) return(0);
  37.     if ((P_Ext[0] == '\0') && (S_Ext[0] != '\0')) return(0);
  38.     if (!I_Match(S_Ext, P_Ext)) return(0);
  39.     return(1);
  40.     }
  41.  
  42.  
  43.  static int
  44. I_Match (char *Str, char *Pat) {
  45.     char           *p, *p1, *p2, Hold;
  46.     int             t;
  47.  
  48.     if ((p1 = strchr(Pat, '*')) == NULL) return(S_Match(Str, Pat, 1));
  49.     if (Pat[0] != '*') {
  50.         *p1 = '\0';
  51.         t = S_Match(Str, Pat, 0);
  52.         *p1 = '*';
  53.         if (!t) return(0);
  54.         }
  55.     if (Pat[strlen(Pat) - 1] != '*') {
  56.         p2 = strrchr(Pat, '*') + 1;
  57.         if (strlen(Str) < strlen(p2)) return(0);
  58.         if (!S_Match(&Str[strlen(Str) - strlen(p2)], p2, 1)) return(0);
  59.         }
  60.  
  61.     p = Str;
  62.     while ((p2 = strchr(++p1, '*')) != NULL) {
  63.         *p2 = '\0';
  64.         Hold = p1[0];
  65.         while ((p = strchr(p, Hold)) != NULL) {
  66.             if (S_Match(p, p1, 0)) break;
  67.             ++p;
  68.             }
  69.         if (p == NULL) return(0);
  70.         p += strlen(p1);
  71.         *p2 = '*';
  72.         p1 = p2;
  73.         }
  74.     return(1);
  75.     }
  76.  
  77.  
  78.  static int
  79. S_Match (char *S, char *P, int Anchor) {
  80.     while ((*P != '\0') && (*S != '\0')) {
  81.         if ((*S == *P) || (*P == '?')) {
  82.             S++;
  83.             P++;
  84.             }
  85.         else return(0);
  86.         }
  87.     if (*P != '\0') return(0);
  88.     if (Anchor && (*S != '\0')) return(0);
  89.     return(1);
  90.     }
  91.